<template>
	<div class="page_user my_home" id="user_address">
		<div class="warp">
			<div class="container">
				<div class="row justify-content-between">
					<div class="col-12 col-md-3">
						<div class="card_menu">
							<!-- 左侧边栏 -->
							<list_admin_menu_user></list_admin_menu_user>
						</div>
					</div>
					<div class="col-12 col-md-9">
						<div class="card_addres pl-2">
							<div class="warp">
								<div class="container-fluid">
									<el-row>
										<div>欢迎来到个人中心</div>
									</el-row>
									<el-row>
																	<el-col v-if="$check_figure('/homestay_information/table')" :span="8">
											<div class="card chart">
																																			<pieChart v-if="list_homestay_information.length" id="list_homestay_information" :list="list_homestay_information" :title="'民宿信息统计'"></pieChart>
												<div v-if="!list_homestay_information.length">民宿信息没有符合条件的数据</div>
																																																																																			</div>
										</el-col>
												<el-col v-if="$check_figure('/homestay_orders/table')" :span="8">
											<div class="card chart">
														<newBarChart v-if="bar_obj_homestay_orders.values.length > 0" id="bar_obj_homestay_orders" :vm="bar_obj_homestay_orders" :title="'民宿订单统计'">
												</newBarChart>
												<div v-if="!bar_obj_homestay_orders.values.length">民宿订单没有符合条件的数据</div>
													</div>
										</el-col>
															<el-col v-if="$check_figure('/homestay_and_catering/table')" :span="8">
											<div class="card chart">
																																										<pieChart v-if="list_homestay_and_catering.length" id="list_homestay_and_catering" :list="list_homestay_and_catering" :title="'民宿餐饮统计'"></pieChart>
												<div v-if="!list_homestay_and_catering.length">民宿餐饮没有符合条件的数据</div>
																																																</div>
										</el-col>
												<el-col v-if="$check_figure('/merchant_billing/table')" :span="8">
											<div class="card chart">
														<newBarChart v-if="bar_obj_merchant_billing.values.length > 0" id="bar_obj_merchant_billing" :vm="bar_obj_merchant_billing" :title="'商家账单统计'">
												</newBarChart>
												<div v-if="!bar_obj_merchant_billing.values.length">商家账单没有符合条件的数据</div>
													</div>
										</el-col>
												<el-col v-if="$check_figure('/catering_orders/table')" :span="8">
											<div class="card chart">
														<newBarChart v-if="bar_obj_catering_orders.values.length > 0" id="bar_obj_catering_orders" :vm="bar_obj_catering_orders" :title="'餐饮订单统计'">
												</newBarChart>
												<div v-if="!bar_obj_catering_orders.values.length">餐饮订单没有符合条件的数据</div>
													</div>
										</el-col>
										</el-row>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import list_admin_menu_user from "@/components/diy/list_admin_menu_user.vue";
	import pieChart from "@/components/charts/pie_chart";
	import newBarChart from "@/components/charts/new_bar_chart";
	export default {
		data() {
			return {
																	list_homestay_information: [],
						bar_obj_homestay_orders: {
					names:[],
					xAxis: [],
					values:[]
				},
												list_homestay_and_catering: [],
						bar_obj_merchant_billing: {
					names:[],
					xAxis: [],
					values:[]
				},
						bar_obj_catering_orders: {
					names:[],
					xAxis: [],
					values:[]
				},
				};
		},
		mounted() {
		},
		methods: {
			async get_nickname(list,flag){
				if (flag) {
					for (let i=0;i<list.length;i++){
						await this.$get(
								"~/api/user/get_obj?user_id="+list[i],
								null,
								(json) => {
									if (json.result) {
										list[i] = json.result.obj.nickname;
									}
								});
					}
				}else {
					for (let i=0;i<list.length;i++){
						await this.$get(
								"~/api/user/get_obj?user_id="+list[i].name,
								null,
								(json) => {
									if (json.result) {
										list[i].name = json.result.obj.nickname;
									}
								});
					}
				}
			},
																															// 获取民宿信息统计图数据
			get_list_homestay_information() {
				let data = {};
								let flag = false;
												let user_group = this.$store.state.user.user_group;
				let user_id = this.$store.state.user.user_id;
				if (user_group!='管理员'){
										let sqlwhere = "(";
																		if (user_group=="商家用户"){
						sqlwhere+= "merchant_users = " + user_id + " or ";
					}
																																																																																																																																																																	if (sqlwhere.length>1){
						sqlwhere = sqlwhere.substr(0,sqlwhere.length-4);
						sqlwhere += ")";
						data.sqlwhere = sqlwhere;
					}
									}
								this.$get("~/api/homestay_information/list_group?groupby=types_of_homestays", data, (json) => {
					if (json.result) {
						var list = json.result.list;
						this.list_homestay_information = list.map((o) => {
							return {
												name: o[1],
												value: o[0]
							};
						});
						if (flag){
							this.get_nickname(this.list_homestay_information,false);
						}
					}
				});
			},
																																																							// 获取民宿订单统计图
			async get_list_homestay_orders() {
				let name_list = [];
				let query_str = "";
																									let group_by_value = "types_of_homestays";
								let flag = false;
												let date_flag = "其他"
																																																														name_list.push("预约天数");
				query_str = query_str+"appointment_days"+","
																	this.bar_obj_homestay_orders.names = name_list
				query_str = query_str.substr(0,query_str.length-1);
				let data = {};
						let user_group = this.$store.state.user.user_group;
				let user_id = this.$store.state.user.user_id;
				if (user_group!='管理员'){
								let sqlwhere = "(";
														if (user_group=="商家用户"){
						sqlwhere+= "merchant_users = " + user_id + " or ";
					}
																																																							if (user_group=="普通用户"){
						sqlwhere+= "ordinary_users = " + user_id + " or ";
					}
																																																		if (sqlwhere.length>1){
						sqlwhere = sqlwhere.substr(0,sqlwhere.length-4);
						sqlwhere += ")";
						data.sqlwhere = sqlwhere;
					}
							}
						await this.$get(
						"~/api/homestay_orders/bar_group?field="+query_str+"&groupby="+group_by_value,
						data,
						(json) => {
							if (json.result) {
								let xAxis = [];
								let values = [];
								json.result.list.map((o) => {
									if (date_flag === "日期") {
										xAxis.push(this.$toTime(o[0] ,"yyyy-MM-dd"));
									}else if (date_flag === "时间") {
										xAxis.push(this.$toTime(o[0] ,"hh:mm:ss"));
									}else if (date_flag === "日长") {
										xAxis.push(this.$toTime(o[0] ,"yyyy-MM-dd hh:mm:ss"));
									}else {
										xAxis.push(o[0]);
									}
									values.push(o.splice(1))
								});
								this.bar_obj_homestay_orders.xAxis = xAxis;
								this.bar_obj_homestay_orders.values = values;
							}
							if (flag){
								this.get_nickname(this.bar_obj_homestay_orders.xAxis,true);
							}
						});
			},
																															// 获取民宿餐饮统计图数据
			get_list_homestay_and_catering() {
				let data = {};
								let flag = false;
												let user_group = this.$store.state.user.user_group;
				let user_id = this.$store.state.user.user_id;
				if (user_group!='管理员'){
										let sqlwhere = "(";
																		if (user_group=="商家用户"){
						sqlwhere+= "merchant_users = " + user_id + " or ";
					}
																																																																																																													if (sqlwhere.length>1){
						sqlwhere = sqlwhere.substr(0,sqlwhere.length-4);
						sqlwhere += ")";
						data.sqlwhere = sqlwhere;
					}
									}
								this.$get("~/api/homestay_and_catering/list_group?groupby=food_name", data, (json) => {
					if (json.result) {
						var list = json.result.list;
						this.list_homestay_and_catering = list.map((o) => {
							return {
												name: o[1],
												value: o[0]
							};
						});
						if (flag){
							this.get_nickname(this.list_homestay_and_catering,false);
						}
					}
				});
			},
																														// 获取商家账单统计图
			async get_list_merchant_billing() {
				let name_list = [];
				let query_str = "";
																																	let group_by_value = "record_month";
								let flag = false;
												let date_flag = "其他"
																						name_list.push("收入金额");
				query_str = query_str+"amount_of_income"+","
															name_list.push("平台抽成");
				query_str = query_str+"platform_drawing"+","
									this.bar_obj_merchant_billing.names = name_list
				query_str = query_str.substr(0,query_str.length-1);
				let data = {};
						let user_group = this.$store.state.user.user_group;
				let user_id = this.$store.state.user.user_id;
				if (user_group!='管理员'){
								let sqlwhere = "(";
														if (user_group=="商家用户"){
						sqlwhere+= "business_user = " + user_id + " or ";
					}
																																																											if (sqlwhere.length>1){
						sqlwhere = sqlwhere.substr(0,sqlwhere.length-4);
						sqlwhere += ")";
						data.sqlwhere = sqlwhere;
					}
							}
						await this.$get(
						"~/api/merchant_billing/bar_group?field="+query_str+"&groupby="+group_by_value,
						data,
						(json) => {
							if (json.result) {
								let xAxis = [];
								let values = [];
								json.result.list.map((o) => {
									if (date_flag === "日期") {
										xAxis.push(this.$toTime(o[0] ,"yyyy-MM-dd"));
									}else if (date_flag === "时间") {
										xAxis.push(this.$toTime(o[0] ,"hh:mm:ss"));
									}else if (date_flag === "日长") {
										xAxis.push(this.$toTime(o[0] ,"yyyy-MM-dd hh:mm:ss"));
									}else {
										xAxis.push(o[0]);
									}
									values.push(o.splice(1))
								});
								this.bar_obj_merchant_billing.xAxis = xAxis;
								this.bar_obj_merchant_billing.values = values;
							}
							if (flag){
								this.get_nickname(this.bar_obj_merchant_billing.xAxis,true);
							}
						});
			},
					// 获取餐饮订单统计图
			async get_list_catering_orders() {
				let name_list = [];
				let query_str = "";
																																	let group_by_value = "food_name";
								let flag = false;
												let date_flag = "其他"
																																														name_list.push("购买数量");
				query_str = query_str+"purchase_quantity"+","
																																									this.bar_obj_catering_orders.names = name_list
				query_str = query_str.substr(0,query_str.length-1);
				let data = {};
						let user_group = this.$store.state.user.user_group;
				let user_id = this.$store.state.user.user_id;
				if (user_group!='管理员'){
								let sqlwhere = "(";
														if (user_group=="商家用户"){
						sqlwhere+= "merchant_users = " + user_id + " or ";
					}
																																																							if (user_group=="普通用户"){
						sqlwhere+= "ordinary_users = " + user_id + " or ";
					}
																																																																				if (sqlwhere.length>1){
						sqlwhere = sqlwhere.substr(0,sqlwhere.length-4);
						sqlwhere += ")";
						data.sqlwhere = sqlwhere;
					}
							}
						await this.$get(
						"~/api/catering_orders/bar_group?field="+query_str+"&groupby="+group_by_value,
						data,
						(json) => {
							if (json.result) {
								let xAxis = [];
								let values = [];
								json.result.list.map((o) => {
									if (date_flag === "日期") {
										xAxis.push(this.$toTime(o[0] ,"yyyy-MM-dd"));
									}else if (date_flag === "时间") {
										xAxis.push(this.$toTime(o[0] ,"hh:mm:ss"));
									}else if (date_flag === "日长") {
										xAxis.push(this.$toTime(o[0] ,"yyyy-MM-dd hh:mm:ss"));
									}else {
										xAxis.push(o[0]);
									}
									values.push(o.splice(1))
								});
								this.bar_obj_catering_orders.xAxis = xAxis;
								this.bar_obj_catering_orders.values = values;
							}
							if (flag){
								this.get_nickname(this.bar_obj_catering_orders.xAxis,true);
							}
						});
			},
			},
		created() {
			setTimeout(() => {
										// 执行民宿信息数据获取
			this.get_list_homestay_information();
					// 执行民宿订单数据获取
			this.get_list_homestay_orders();
								// 执行民宿餐饮数据获取
			this.get_list_homestay_and_catering();
					// 执行商家账单数据获取
			this.get_list_merchant_billing();
					// 执行餐饮订单数据获取
			this.get_list_catering_orders();
				}, 1000);
		},
		components: {
			pieChart,
			newBarChart,
			list_admin_menu_user
		},
	};
</script>

<style scoped>
	.container {
		min-height: 800px;
	}
</style>
